package org.moonlight.jvm.rtda.share.heap;

import lombok.EqualsAndHashCode;
import org.moonlight.jvm.rtda.exclusive.Slot;
import org.moonlight.jvm.rtda.share.Object;

import java.io.Serializable;

/**
 * 存储 类变量 和 实例变量
 *
 * @author Moonlight
 * @createTime 2023/8/29 16:57
 **/
@EqualsAndHashCode
public class Slots implements Serializable {

    private Slot[] slots;

    public Slots(int slotCount) {
        if (slotCount > 0) {
            this.slots = new Slot[slotCount];
            for (int i = 0; i < slotCount; i++) {
                this.slots[i] = new Slot();
            }
        }
    }

    public Slots(Slot[] slots) {
        if (slots == null || slots.length == 0) {
            throw new RuntimeException("slots 不能为空");
        }
        this.slots = slots;
    }

    public void setInt(int idx, int val) {
        this.slots[idx].setNum(val);
    }

    public int getInt(int idx) {
        return this.slots[idx].getNum().intValue();
    }

    public void setFloat(int idx, float val) {
        this.slots[idx].setNum(val);
    }

    public float getFloat(int idx) {
        return this.slots[idx].getNum().floatValue();
    }

    public void setLong(int idx, long val) {
//        this.slots[idx].setNum((int) val);
//        this.slots[idx + 1].setNum((int) (val >> 32));
        this.slots[idx].setNum(val);
    }

    public long getLong(int idx) {
//        int low = this.slots[idx].getNum().intValue();
//        int high = this.slots[idx + 1].getNum().intValue();
//        return (long) high << 32 | (long) low;
        return this.slots[idx].getNum().longValue();
    }

    public void setDouble(int idx, double val) {
//        this.slots[idx].setNum((int) val);
//        this.slots[idx + 1].setNum((int) (val >> 32));
        this.slots[idx].setNum(val);
    }

    public double getDouble(int idx) {
//        int low = this.slots[idx].getNum().intValue();
//        int high = this.slots[idx + 1].getNum().intValue();
//        return (double) high << 32 | (double) low;
        return this.slots[idx].getNum().doubleValue();
    }

    public void setRef(int idx, Object ref) {
        this.slots[idx].setRef(ref);
    }

    public Object getRef(int idx){
        return this.slots[idx].getRef();
    }

    public int getLength() {
        return this.slots.length;
    }

    public Slot[] getSlots() {
        return slots;
    }
}
